﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModule;
using uCoreClasses;
using MBB_Store_Sales_Report;
using DataManager;
using UnityScriptEngine;
namespace MBBStoreClient
{
    public partial class frmMain : Form
    {
        Sync sy = new Sync();
        public frmMain()
        {
            InitializeComponent();

            this.Resize += new EventHandler(frmMain_Resize);
            this.Load += new EventHandler(frmMain_Load);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);

            //Menu
            mnuSalesOrderEntry.Click += new EventHandler(mnuSalesOrderEntry_Click);
            mnuSettings.Click += new EventHandler(mnuSettings_Click);
            mnuCloseAll.Click += new EventHandler(mnuCloseAll_Click);
            mnuExit.Click += new EventHandler(mnuExit_Click);
            mnuUpdate.Click += new EventHandler(mnuUpdate_Click);
            mnuViewPriceList.Click += new EventHandler(mnuViewPriceList_Click);
            mnuDailySales.Click += new EventHandler(mnuDailySales_Click);
            mnuAboutThis.Click += new EventHandler(mnuAboutThis_Click);
            mnuUserManager.Click += new EventHandler(mnuUserManager_Click);
            mnuAccount.Click += new EventHandler(mnuAccount_Click);
            mnuLogUser.Click += new EventHandler(mnuLogUser_Click);
            mnuCashDenomination.Click += new EventHandler(mnuCashDenomination_Click);
            mnuCashReg.Click += new EventHandler(mnuCashReg_Click);
            mnuChecklist.Click += new EventHandler(mnuChecklist_Click);
            mnuChequeEntry.Click += new EventHandler(mnuChequeEntry_Click);
            mnuExpensesEntry.Click += new EventHandler(mnuExpensesEntry_Click);
            mnuExpensesList.Click += new EventHandler(mnuExpensesList_Click);
            mnuProductContentManagement.Click += new EventHandler(mnuProductContentManagement_Click);

          
            //mnuChargeInvoicePaymentsEntry.Click += new EventHandler(mnuChargeInvoicePaymentsEntry_Click);
            mnuChargeInvoiceEntry.Click += new EventHandler(mnuChargeInvoiceEntry_Click);
            //mnuChargeInvoiceViewer.Click += new EventHandler(mnuChargeInvoiceViewer_Click);
            mnuStatementOfAccount.Click += new EventHandler(mnuStatementOfAccount_Click);
            mnuCheckStatusEntry.Click += new EventHandler(mnuCheckStatusEntry_Click);
            mnuCheckStatusViewer.Click += new EventHandler(mnuCheckStatusViewer_Click);
            mnuUpload.Click += new EventHandler(mnuUpload_Click);
            //mnuPaintMix.Click += new EventHandler(mnuPaintMix_Click);
            mnuStockLogViewer.Click += new EventHandler(mnuStockLogViewer_Click);
            mnuStockManagement.Click += new EventHandler(mnuStockManagement_Click);
            mnuSearchProducts.Click += new EventHandler(mnuSearchProducts_Click);
            mnuStockInventoryViewer.Click += new EventHandler(mnuStockInventoryViewer_Click);
            mnuInventoryMode.Click += new EventHandler(mnuInventoryMode_Click);
            mnuExportProducts.Click += new EventHandler(mnuExportProducts_Click);
            mnuImportInventory.Click += new EventHandler(mnuImportInventory_Click);
            mnuPurchaseOrderEntry.Click += new EventHandler(mnuPurchaseOrderEntry_Click);
            mnuPurchaseOrderManager.Click += new EventHandler(mnuPurchaseOrderManager_Click);
            mnuSupplierProfiles.Click += new EventHandler(mnuSupplierProfiles_Click);
            mnuPurchaseOrderLog.Click += new EventHandler(mnuPurchaseOrderLog_Click);
            //MainSlider
            sldMain.ItemAdded += new EventHandler(sldMain_ItemAdded);
            sldMain.ItemRemoved += new EventHandler(sldMain_ItemRemoved);
            sldMain.AnimationCompleted += new EventHandler(sldMain_AnimationCompleted);

            //Sync
            sy.ProcessCompleted += new ModuleEventHandler(sy_ProcessCompleted);
            sy.ProcessError += new ModuleEventHandler(sy_ProcessError);
            sy.ProcessUpdate += new ModuleEventHandler(sy_ProcessUpdate);

            //additional
            customerProfileEntryToolStripMenuItem.Click += new EventHandler(customerProfileEntryToolStripMenuItem_Click);
            customersProfileViewerToolStripMenuItem.Click += new EventHandler(customersProfileViewerToolStripMenuItem_Click);
            mnuChargeInvoiceSummary.Click += new EventHandler(mnuChargeInvoiceSummary_Click);
            mnuDeliverySummaryEntry.Click += new EventHandler(mnuDeliverySummaryEntry_Click);
            
        }

        void mnuDeliverySummaryEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmDeliverySummaryManager frm = new frmDeliverySummaryManager();
            frm.Initialize("Delivery Summary Manager");
            sldMain.AddItem(frm.PanelForm, "Delivery Summary Manager");
        }

        void mnuChargeInvoiceSummary_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmNewReceivable frm = new frmNewReceivable();
            frm.Initialize("Charged Invoice Summary");
            sldMain.AddItem(frm.PanelForm, "Charged Invoice Summary");
        }

        void mnuPurchaseOrderLog_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmPurchaseOrderLogViewer frm = new frmPurchaseOrderLogViewer();
            frm.Initialize("Supplier Profile");
            sldMain.AddItem(frm.PanelForm, "Supplier Profile");
        }

        void mnuSupplierProfiles_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmSupplierProfile frm = new frmSupplierProfile();
            frm.Initialize("Supplier Profile");
            sldMain.AddItem(frm.PanelForm, "Supplier Profile");
        }

        void mnuPurchaseOrderManager_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmPurchaseOrderManager frm = new frmPurchaseOrderManager();
            frm.Initialize("Purchase Order Manager");
            sldMain.AddItem(frm.PanelForm, "Purchase Order Manager");
        }

        void mnuPurchaseOrderEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmPurchaseOrderEntry frm = new frmPurchaseOrderEntry();
            frm.Initialize("Purchase Order Entry");
            sldMain.AddItem(frm.PanelForm, "Purchase Order Entry");
        }

        void mnuImportInventory_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmInventoryImport frm = new frmInventoryImport();
            frm.Initialize("Import Saved Inventory Data");
            sldMain.AddItem(frm.PanelForm, "Import Saved Inventory Data");
        }

        void mnuExportProducts_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Export Products Data";
            dlg.Filter = "All|*.*|Exported Products Data|*.epd";
            dlg.FilterIndex = 2;
            dlg.AddExtension = true;
            dlg.CheckPathExists = true;
            dlg.OverwritePrompt = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                frmExportProductsData frm = new frmExportProductsData(dlg.FileName);
                frm.ShowDialog();
            }

        }

        void mnuInventoryMode_Click(object sender, EventArgs e)
        {
            //if (!Common.UserLogged) return;
            frmInventoryModule frm = new frmInventoryModule();
            frm.Initialize("Inventory Mode");
            sldMain.AddItem(frm.PanelForm, "Inventory Mode");
        }

        void mnuStockInventoryViewer_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmInventoryViewer frm = new frmInventoryViewer();
            frm.Initialize("Stock Inventory Viewer");
            sldMain.AddItem(frm.PanelForm, "Stock Inventory Viewer");
        }

        void mnuSearchProducts_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmProductSearch frm = new frmProductSearch();
            frm.Initialize("Product Search");
            sldMain.AddItem(frm.PanelForm, "Product Search");
        }

        void mnuStockManagement_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmStockManager frm = new frmStockManager();
            frm.Initialize("Stock Management");
            sldMain.AddItem(frm.PanelForm, "Stock Management");
        }

        void mnuStockLogViewer_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmLogViewer frm = new frmLogViewer();
            frm.Initialize("Stock Log Viewer");
            sldMain.AddItem(frm.PanelForm, "Stock Log Viewer");
        }

        void customersProfileViewerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmProfileViewer frm = new frmProfileViewer();
            frm.Initialize("View Customer's Profile");
            sldMain.AddItem(frm.PanelForm, "View Customer's Profile");
        }

        void customerProfileEntryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmProfileEntry frm = new frmProfileEntry();
            frm.Initialize("Customer's Profile Entry",true);
            sldMain.AddItem(frm.PanelForm, "Customer's Profile Entry");
        }

        void mnuPaintMix_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmPaintMixing frm = new frmPaintMixing();
            frm.Initialize("Paint Mix Entry");
            sldMain.AddItem(frm.PanelForm, "Paint Mix Entry");
        }


        void mnuUpload_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmUpload frm = new frmUpload();
            frm.Initialize("Upload Data Online");
            sldMain.AddItem(frm.PanelForm, "Upload Data Online");
        }

        void mnuCheckStatusViewer_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmCheckStatusViewer frm = new frmCheckStatusViewer();
            frm.Initialize("Check Status Entry");
            sldMain.AddItem(frm.PanelForm, "Check Status Entry");
        }

        void mnuCheckStatusEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmCheckStatusEntry frm = new frmCheckStatusEntry();
            frm.Initialize("Check Status Entry");
            sldMain.AddItem(frm.PanelForm, "Check Status Entry");
        }

        void mnuStatementOfAccount_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmStatementOfAccountManager frm = new frmStatementOfAccountManager();
            frm.Initialize("Statement Of Account Report");
            sldMain.AddItem(frm.PanelForm, "Statent of Account Report");
        }

        

        void mnuChargeInvoiceEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmChargedInvoiceEntry frm = new frmChargedInvoiceEntry();
            frm.Initialize("Charge Invoice");
            sldMain.AddItem(frm.PanelForm, "Charge Invoice");
        }

       

       

        void mnuProductContentManagement_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmContentManagement frm = new frmContentManagement();
            frm.Initialize("Content Management");
            sldMain.AddItem(frm.PanelForm, "Content Management");
        }

        void mnuExpensesList_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmExpensesList frm = new frmExpensesList();
            frm.Initialize("Expenses List");
            sldMain.AddItem(frm.PanelForm, "Expenses List");
        }

        void mnuExpensesEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmExpensesEntry frm = new frmExpensesEntry();
            frm.Initialize("Expenses Entry");
            sldMain.AddItem(frm.PanelForm, "Expenses Entry");
        }

        void mnuChequeEntry_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmChequeEntry frm = new frmChequeEntry();
            frm.Initialize("Cheque Entry");
            sldMain.AddItem(frm.PanelForm, "Cheque Entry");
        }
        void mnuChecklist_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmCheckList frm = new frmCheckList();
            frm.Initialize("Check List");
            sldMain.AddItem(frm.PanelForm, "Check List");
        }
        void mnuCashReg_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmCashRegisteredList frm = new frmCashRegisteredList();
            frm.Initialize("Cash Registered List");
            sldMain.AddItem(frm.PanelForm, "Cash Registered List");
        }
        void mnuCashDenomination_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmCashDenomination frm = new frmCashDenomination();
            frm.Initialize("Cash Denomination");
            sldMain.AddItem(frm.PanelForm, "Cash Denomination");
        }
        void mnuAccount_Click(object sender, EventArgs e)
        {
            if (!Common.UserLogged) return;
            frmMyAccount frm = new frmMyAccount();
            frm.Initialize("My Account");
            sldMain.AddItem(frm.PanelForm, "My Account");
        }

        void mnuLogUser_Click(object sender, EventArgs e)
        {
            if (Common.UserLogged)
            {
                DialogResult dlg = MessageBox.Show("Are you sure to logout?", "Logout",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    Common.UserLogged = false;
                    mnuSales.Enabled = false;
                    mnuUserManager.Enabled = false;
                    mnuAccount.Enabled = false;
                    mnuUpdate.Enabled = false;
                    mnuInventory.Enabled = false;
                    mnuPurchase.Enabled = false;
                    mnuCharge.Enabled = false;
                    mnuLogUser.Text = "Login";

                    sldMain.Controls.Clear();
                }
                else
                {
                    return;
                }
            }
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
        }
        void mnuUserManager_Click(object sender, EventArgs e)
        {
            frmUserManager frm = new frmUserManager();
            frm.Initialize("User Manager");
            sldMain.AddItem(frm.PanelForm, "User Manager");
        }
        void mnuAboutThis_Click(object sender, EventArgs e)
        {
            frmAboutThis frm = new frmAboutThis();
            frm.Initialize("About This");
            frm.FrameSliderVersion(sldMain.Version);
            sldMain.AddItem(frm.PanelForm, "About This");
        }
        void mnuDailySales_Click(object sender, EventArgs e)
        {
            if (mnuSales.Enabled)
            {
                frmDailySales frm = new frmDailySales();
                frm.Initialize("Daily Sales Report");
                sldMain.AddItem(frm.PanelForm, "Daily Sales Report");
            }
        }

        void mnuViewPriceList_Click(object sender, EventArgs e)
        {
            frmPriceListViewer frm = new frmPriceListViewer();
            frm.Initialize("Price List Viewer");
            sldMain.AddItem(frm.PanelForm, "Price List Viewer");
        }
        void mnuUpdate_Click(object sender, EventArgs e)
        {
            if (!mnuUpdate.Enabled) return;
            frmUpdate frm = new frmUpdate();
            frm.Initialize("Data Update");
            sldMain.AddItem(frm.PanelForm, "Data Update");
        }
        void mnuSalesOrderEntry_Click(object sender, EventArgs e)
        {
            if (!mnuSales.Enabled) return;
            frmSalesEntry frm = new frmSalesEntry();
            frm.Initialize("Sales Order");
            sldMain.AddItem(frm.PanelForm, "Sales Order");
        }
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void mnuSettings_Click(object sender, EventArgs e)
        {
            frmSettings frm = new frmSettings();
            frm.Initialize("Connection Settings");
            sldMain.AddItem(frm.PanelForm, "Connection Settings");
        }
        void mnuCloseAll_Click(object sender, EventArgs e)
        {
           
            if (sldMain.ItemCount > 0)
            {
                DialogResult dlg = MessageBox.Show("This will close all session. Are you sure?", "Close Sessions", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    sldMain.Controls.Clear();
                }
            }
        }

        void sldMain_AnimationCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < mnuSession.DropDownItems.Count; i++)
            {
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
        }
        void sldMain_ItemRemoved(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuSession.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuSession.DropDownItems.Add(i + " - " + buf[i], null, new EventHandler(Session_Click));
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            if(sldMain.ItemCount > 0)mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_ItemAdded(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuSession.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuSession.DropDownItems.Add(i + " - " + buf[i], null, new EventHandler(Session_Click));
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void Session_Click(object sender, EventArgs e)
        {
            int index = mnuSession.DropDownItems.IndexOf((ToolStripItem)sender);
            for (int i = 0; i < mnuSession.DropDownItems.Count; i++)
            {
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            sldMain.MoveTo(index);
            mnuSession.DropDownItems[index].ForeColor = Color.RoyalBlue;
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.Left = Screen.PrimaryScreen.Bounds.X;
            this.Top = Screen.PrimaryScreen.Bounds.Y;
            frmMain_Resize(sender, e);
            Common.parent = this;
            LoadSettings();
            MenuState(false);
            
        }
        void frmMain_Resize(object sender, EventArgs e)
        {
            sldMain.Left = 12;
            sldMain.Width = this.Width - sldMain.Left - 13;
            sldMain.Height = this.Height - sldMain.Top - 32;
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlg = MessageBox.Show("Are you sure to exit the application?",
               "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (dlg == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        void sy_ProcessUpdate(string procCode, string msg)
        {

        }
        void sy_ProcessError(string procCode, string msg)
        {
            uThreader t = new uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg, procCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
                MenuState(true);
            });
        }
        void sy_ProcessCompleted(string procCode, string msg)
        {
            uThreader t = new uThreader();
            t.SafeCall(this, delegate
            {
                MenuState(true);
                if (Common.Updates.Length > 0)
                {
                    MessageBox.Show("Updated Data is available, update will now start", "Data Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmUpdate frm = new frmUpdate();
                    frm.Initialize("Data Update");
                    for (int i = 0; i < Common.Updates.Length; i++)
                    {
                        frm.AddData("Update for " + Common.Updates[i].fRemarks + " is available.");
                    }
                    sldMain.AddItem(frm.PanelForm, "Data Update");
                }
            });
        }
        void MenuState(bool s)
        {
            mnuUpdate.Enabled = s;
            mnuSales.Enabled = s;
            mnuInventory.Enabled = s;
            mnuPurchase.Enabled = s;
        }
        //Methods
        void LoadSettings()
        {
            uTesselation tes = new uTesselation("lowgate", 256);
            SettingType[] st = new SettingType[0];
            try
            {
                st = Common.ReadSettings();

                Common.lServer = st[0].Node[0].Value;
                Common.lDatabase = st[0].Node[1].Value;
                Common.lUID = st[0].Node[2].Value;
                Common.lPWD = tes.Tesselate(st[0].Node[3].Value);

                Common.lStoreID = st[1].Node[0].Value;
                Common.lStoreName = st[1].Node[1].Value;
                Common.lStoreAddress = st[1].Node[2].Value;

                Common.oServer = st[2].Node[0].Value;
                Common.oDatabase = st[2].Node[1].Value;
                Common.oUID = st[2].Node[2].Value;
                Common.oPWD = tes.Tesselate(st[2].Node[3].Value);

                UnityScriptEngine.UnityProcessor.Settings.APP_ID = "MBB Store Client";
                UnityScriptEngine.UnityProcessor.Settings.APP_Name = "MBB Store Client";
                UnityScriptEngine.UnityProcessor.Settings.DBPU_Server = Common.lServer; 
                UnityScriptEngine.UnityProcessor.Settings.DBPU_Database = Common.lDatabase;
                UnityScriptEngine.UnityProcessor.Settings.DBPU_UID = Common.lUID;
                UnityScriptEngine.UnityProcessor.Settings.DBPU_PWD = Common.lPWD;

                this.Text = "MBB Store Client - " + Common.lStoreName;

                Threader t = new Threader();
                t.MethodCall(InitializeTable);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read settings");
            }
        }
        public void LoginUser(string UID)
        {
            Common.Username = UID;
            mnuSales.Enabled = true;
            mnuAccount.Enabled = true;
            mnuUpdate.Enabled = true;
            mnuInventory.Enabled = true;
            mnuPurchase.Enabled = true;
            mnuCharge.Enabled = true;
            if (UID == "admin")
            {
                mnuUserManager.Enabled = true;
            }
            mnuLogUser.Text = "Logout " + Common.Username;
            //sy.CheckUpdate();
        }
        public void EditCheck(ChequeEntryCommit check)
        {
            frmChequeEntry frmC = new frmChequeEntry();
            //get the value of the struct
            frmC.oldData(check);
            frmC.buttonChange02();
            frmC.Initialize("Check Update");
            sldMain.AddItem(frmC.PanelForm, "Check Update");
        }

        public void EditExpenses(ExpensesEntryCommit expenses)
        {
            frmExpensesEntry frmE = new frmExpensesEntry();
            //get the value of the struct
            frmE.oldData(expenses);
            frmE.buttonChanged01();
            frmE.Initialize("Expenses Update");
            sldMain.AddItem(frmE.PanelForm, "Expenses Update");
        }
        public void EditProfile(ProfileInfo profile)
        {
            frmProfileEntry frm = new frmProfileEntry();
            frm.OldInfo(profile);
            frm.Initialize("Profile - " + profile.name, false);
            sldMain.AddItem(frm.PanelForm, "Profile - " + profile.name);
        }

        public void ViewPO(string number)
        {
            frmPurchaseOrderEntry frm = new frmPurchaseOrderEntry(number);
            frm.Initialize("Purchase Order #" + number);
            sldMain.AddItem(frm.PanelForm, "Purchase Order #" + number);
        }
        public void ViewChargeInvoice(string number)
        {
            frmChargedInvoiceEntry frm = new frmChargedInvoiceEntry(number);
            frm.Initialize("Charged Invoice #" + number);
            sldMain.AddItem(frm.PanelForm, "Charged Invoice #" + number);
        }
        void InitializeTable()
        {
            UnityProcessor proc = new UnityProcessor();
            proc.ProcessError += new ProcessHandler(delegate(string id, string msg)
            {
                MessageBox.Show(msg);
            });
            UnityScript u = new UnityScript("CHECKTABLEPARITY", 5);
            u.Script_Add(Script.DBPUX_BEGIN);
            //tblpurchaseorder
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS tblpurchaseorder (" +
                                                  "fID bigint(20) NOT NULL AUTO_INCREMENT," +
                                                  "fNumber varchar(255) NOT NULL," +
                                                  "fRefNum text NOT NULL," +
                                                  "fDate date NOT NULL," +
                                                  "fTerms float NOT NULL," +
                                                  "fStamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                  "PRIMARY KEY (fID,fNumber)," +
                                                  "UNIQUE KEY fNumber (fNumber)" +
                                                ") ENGINE=InnoDB");
            //tblpurchaseordergenerate
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS `tblpurchaseordergenerate` (" +
                                                  "`fID` bigint(20) NOT NULL AUTO_INCREMENT," +
                                                  "`fCurNum` bigint(20) NOT NULL," +
                                                  "`fCurSeries` varchar(255) NOT NULL," +
                                                  "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                  "PRIMARY KEY (`fID`)" +
                                                ") ENGINE=InnoDB AUTO_INCREMENT=0");
            
            //tblpurchaseorderitems
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS `tblpurchaseorderitems` (" +
                                                  "`fID` bigint(20) NOT NULL AUTO_INCREMENT," +
                                                  "`fNumber` varchar(255) NOT NULL," +
                                                  "`fBarcode` text NOT NULL," +
                                                  "`fQty` float NOT NULL," +
                                                  "`fPrice` float NOT NULL," +
                                                  "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                  "PRIMARY KEY (`fID`)" +
                                                ") ENGINE=InnoDB");
            //tblpurchaseorderlog
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS `tblpurchaseorderlog` (" +
                                                  "`fID` bigint(20) NOT NULL AUTO_INCREMENT," +
                                                  "`fUser` text NOT NULL," +
                                                  "`fMsg` text NOT NULL," +
                                                  "`fDate` datetime NOT NULL," +
                                                  "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                  "PRIMARY KEY (`fID`)" +
                                                ") ENGINE=InnoDB");
            //tblpurchaseorderpayments
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS `tblpurchaseorderpayments` (" +
                                                "`fID` bigint(20) NOT NULL AUTO_INCREMENT," +
                                                "`fNumber` varchar(255) NOT NULL," +
                                                "`fDate` date NOT NULL," +
                                                "`fPaymentAmount` float NOT NULL," +
                                                "`fType` text NOT NULL," +
                                                "`fBank` text," +
                                                "`fAccountName` text," +
                                                "`fAccountNum` text," +
                                                "`fAmount` float DEFAULT NULL," +
                                                "`fIssueDate` date DEFAULT NULL," +
                                                "`fDueDate` date DEFAULT NULL," +
                                                "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                "PRIMARY KEY (`fID`)" +
                                                ") ENGINE=InnoDB");
            //tblsupplierprofile
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE IF NOT EXISTS `tblsupplierprofile` (" +
                                                  "`fID` bigint(20) NOT NULL AUTO_INCREMENT," +
                                                  "`fRefNum` varchar(255) NOT NULL," +
                                                  "`fName` text NOT NULL," +
                                                  "`fAddress` text," +
                                                  "`fContactNum` text," +
                                                  "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
                                                  "PRIMARY KEY (`fID`,`fRefNum`)," +
                                                  "UNIQUE KEY `fRefNum` (`fRefNum`)" +
                                                ") ENGINE=InnoDB");
            u.Script_Add(Script.DBPUX_GENERIC, "DROP TABLE IF EXISTS tbluploads");
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TABLE `tbluploads` (" +
                                              "`fID` bigint(20) NOT NULL," +
                                              "`fUpdateKey` text," +
                                              "`fPKey` text," +
                                              "`fParentTable` text," +
                                              "`fParentKey` text," +
                                              "`fRemarks` text," +
                                              "`fFlag` text," +
                                              "`fStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                                              "PRIMARY KEY (`fID`)" +
                                            ") ENGINE=InnoDB");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('1', 'tblCashRegistry', 'fDate', null, null, 'Cash Registry', null, '2012-08-21 00:15:02');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('2', 'tblCheque', 'fDateReceived', null, null, 'Cheques', null, '2012-08-21 00:15:11');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('3', 'tblExpenses', 'fDate', null, null, 'Expenses', null, '2012-08-21 00:15:14');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('4', 'tblSalesDetail', 'fDate', null, null, 'Daily Sales', null, '2012-08-21 00:15:17');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('5', 'tblSalesItem', 'fInvoice', 'tblSalesDetail', 'fDate', 'Daily Sales Item', null, '2012-08-21 00:15:31');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('6', 'tblChargeBalance', null, null, null, 'Charge Balance', null, '2013-02-22 03:35:31');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('7', 'tblChargePayments', null, null, null, 'Charge Payments', null, '2013-02-22 03:36:28');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('8', 'tblSalesDetail', 'fInvoiceType', null, null, 'Charge Invoice', null, '2013-02-22 03:37:43');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('9', 'tblSalesItem', 'fInvoice', 'tblSalesDetail', 'fInvoiceType', 'Charge Invoice Item', null, '2013-02-23 01:44:56');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('11', 'tblProfile', null, null, null, 'Profile', null, '2013-02-23 01:45:58');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('13', 'tblProductStockWhole', null, null, null, 'Product Stock Whole', null, '2013-02-23 01:48:38');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('14', 'tblStockLog', null, null, null, 'Stock Log', null, '2013-02-23 01:48:55');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('15', 'tblpurchaseorder', null, null, null, 'tblpurchaseorder', null, '2013-02-23 01:48:55');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('16', 'tblpurchaseorderitems', null, null, null, 'tblpurchaseorderitems', null, '2013-02-23 01:48:55');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('17', 'tblpurchaseorderpayments', null, null, null, 'tblpurchaseorderpayments', null, '2013-02-23 01:48:55');");
            u.Script_Add(Script.DBPUX_GENERIC, "INSERT INTO `tbluploads` VALUES ('18', 'tblsupplierprofile', null, null, null, 'tblsupplierprofile', null, '2013-02-23 01:48:55');");
            u.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(u);
        }
    }
}
